<?php
/************************************
*   新浪相册 7.0
*   @file:mc_cache.php
*   @intro: 缓存控制
*   @author:shaopu@
*   @date:Mon Nov 24 16:01:16 CST 2008
************************************/

class mc_cache
{
    /**
     * 读取用户数据缓存
     *
     * @param int $uid
     * @param array $aInfo
     * @return bool
     */
    public function get_user_cache($uid , &$aInfo)
    {
        if(!K5_ENABLE_MC_CACHE)
            return false;
            
        $oMc = K::mc('user_cache');
        $rs = $oMc->get($uid , $aInfo);
        //忽略容错
        
        //检查数据，并恢复数组的键
        $db_config = K::config('db_config');
        $db_config = $db_config['user'];
        if(count($aInfo) <> count($db_config['fields_common']))
            return false;
        else 
            $aInfo = array_combine($db_config['fields_common'] , $aInfo);
        
        if($aInfo['user_uid'] <> $uid)
            return false;
        
        return true;
    }
    /**
     * 写入用户数据缓存
     *
     * @param int $uid
     * @param array $aInfo
     * @return bool
     */
    public function set_user_cache($uid , $aInfo)
    {
        if(!K5_ENABLE_MC_CACHE)
            return true;
        
        //去掉数组的键，以节约MC空间
        $db_config = K::config('db_config');
        $db_config = $db_config['user'];
        if(count($db_config['fields_common']) <> count($aInfo))//字段数与预定义不同，不能保存
            return false;
        else 
            $aInfo = array_values($aInfo);//干掉KEY，只存VALUE
            
            
        $oMc = K::mc('user_cache');
        $rs = $oMc->set($uid , $aInfo);
        
        return true;
    }
    /**
     * 删除用户数据缓存
     *
     * @param int $uid
     * @return bool
     */
    public function del_user_cache($uid)
    {
        if(!K5_ENABLE_MC_CACHE)
            return true;
           
        $oMc = K::mc('user_cache');
        $oMc->delete($uid);
        
        return true;
    }
    
    /**
     * 写入图片数据缓存
     *
     * @param string $pic_id
     * @param array $aInfo
     * @return bool
     */
    public function set_photo_cache($pic_id , $aInfo)
    {
        if(!K5_ENABLE_MC_CACHE)
            return true;
        $uid = res2uid($pic_id);
        //去掉数组的键，以节约MC空间
        $db_config = K::config('db_config');
        $db_config = $db_config['photo'];
        if(count($db_config['fields_detail']) <> count($aInfo))//字段数与预定义不同，不能保存
            return false;
        else 
            $aInfo = array_values($aInfo);//干掉KEY，只存VALUE
            
            
        $oMc = K::mc('photo_cache');
        $rs = $oMc->set($pic_id , $aInfo ,$uid);
        
        return true;
    }
    /**
     * 读图片信息缓存
     * @param string $pic_id
     * @param array $aInfo
     * @return boolean
     */
    public function get_photo_cache($pic_id , &$aInfo)
    {
        if(!K5_ENABLE_MC_CACHE)
            return false;
            
        $uid = res2uid($pic_id);   
        $oMc = K::mc('photo_cache');
        $rs = $oMc->get($pic_id , $aInfo,$uid);
        //忽略容错
        
        //检查数据，并恢复数组的键
        $db_config = K::config('db_config');
        $db_config = $db_config['photo'];
        if(count($aInfo) <> count($db_config['fields_detail']))
            return false;
        else 
            $aInfo = array_combine($db_config['fields_detail'] , $aInfo);
        
        if($aInfo['pic_uid'] <> $uid)
            return false;
        
        return true;
    }
    
    public function get_multi_photo_cache($arr_pic_id , &$arr_photo_info , $uid)
    {
        $arr_photo_info = array();
        if(!K5_ENABLE_MC_CACHE)
            return false;
        
        $oMc = K::mc('photo_cache');
        $rs = $oMc->get_multi($arr_pic_id , $arr_info,$uid);
        
        //检查数据，并恢复数组的键
        $db_config = K::config('db_config');
        $db_config = $db_config['photo'];
        foreach ($arr_info as $photo_info)
        {
            if(count($photo_info) == count($db_config['fields_detail']))
            {
                $photo_info = array_combine($db_config['fields_detail'] , $photo_info);
                if($photo_info['pic_uid'] == $uid)
                {
                    $arr_photo_info[] = $photo_info;
                }
            }
        }
        
        return true;
    }
    /**
     * 删除图片信息缓存
     *
     * @param array $arr_pic_id
     * @return boolean
     */
    public function del_photo_cache($arr_pic_id)
    {
        if(!K5_ENABLE_MC_CACHE)
            return true;
             
            
        $oMc = K::mc('photo_cache');
        foreach ($arr_pic_id as $pic_id)
        {
            $uid = res2uid($pic_id);
            $oMc->delete($pic_id , $uid);
        }
        
        
        return true;
    }
}
?>